
package com.erp.common.web;

import java.util.HashMap;
import java.util.LinkedList;

import javax.servlet.http.HttpSession;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.util.ProjectCommonUtil;

public class RemarkTextPopup {
 
    private HttpSession session;

    private final LinkedList keyList = new LinkedList();

    private String fieldId;

    private String fieldValue;

    private String pageEventSrc;

    private String editColumnName;

    private String openerPath;

    private boolean readOnly = false;

    private String sessionName;

    private boolean selectRemarkText = false;

    public RemarkTextPopup() {
    }

    public RemarkTextPopup(HttpSession session) {
        this();
        this.setSession(session);
    }

    protected LinkedList getKeyList() {
        return keyList;
    }

    public String getFieldId() {
        return fieldId;
    }

    public String getPageEventSrc() {
        return pageEventSrc;
    }

    public String getEditColumnName() {
        return editColumnName;
    }

    public String getOpenerPath() {
        return openerPath;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public String getSessionName() {
        return sessionName;
    }

    public String getFieldValue() {
        return fieldValue;
    }

    public void setFieldValue(String fieldValue) {
        this.fieldValue = fieldValue;
    }

    public RemarkTextPopup addKey(Object key, Object value) {
        this.keyList.add(key);
        this.keyList.add(value);
        return this;
    }

    public RemarkTextPopup setFieldId(String fieldId) {
        this.fieldId = fieldId;
        return this;
    }

    public RemarkTextPopup setPageEventSrc(String pageEventSrc) {
        this.pageEventSrc = pageEventSrc;
        return this;
    }

    public RemarkTextPopup setEditColumnName(String editColumnName) {
        this.editColumnName = editColumnName;
        return this;
    }

    public RemarkTextPopup setSessionName(String sessionName) {
        this.sessionName = sessionName;
        return this;
    }

    public RemarkTextPopup setOpenerPath(String functionId) {
        this.openerPath = functionId;
        return this;
    }

    public RemarkTextPopup setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
        return this;
    }

    public RemarkTextPopup setSession(HttpSession session) {
        this.session = session;
        return this;
    }

    public HttpSession getSession() {
        return this.session;
    }

    public boolean isSelectRemarkText() {
        return this.selectRemarkText;
    }

    public RemarkTextPopup setSelectRemarkText(boolean selectRemarkText) {
        this.selectRemarkText = selectRemarkText;
        return this;
    }

    protected String getParametersSessionName() {
        return this.isSelectRemarkText() ? ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS : ProjectConstants.SESSION_REMARK_TEXT_PARAMETERS;
    }

    protected void assertFields() {
        ProjectCommonUtil.assertNotNull(this.getSession(), "session cannot be null");
        ProjectCommonUtil.assertNotNull(this.getSessionName(), "session name cannot be null");
        ProjectCommonUtil.assertNotNull(this.getOpenerPath(), "opener path cannot be null");
        ProjectCommonUtil.assertNotNull(this.getFieldId(), "field id cannot be null");
        ProjectCommonUtil.assertNotNull(this.getPageEventSrc(), "page event src cannot be null");
        ProjectCommonUtil.assertNotNull(this.getEditColumnName(), "edit column name cannot be null");
    }

    public void build() {
        this.assertFields();
        HashMap params = new HashMap();
        params.put(ProjectConstants.SESSION_SESSION_VALUE, this.getSessionName());
        params.put(ProjectConstants.SESSION_OPENER_PATH, this.getOpenerPath());
        params.put(ProjectConstants.SESSION_KEY_LIST, this.getKeyList());
        if (this.isReadOnly()) params.put(ProjectConstants.SESSION_SIMPLE_READ_ONLY, ProjectConstants.GENERIC_YES);
        params.put(ProjectConstants.SESSION_FIELD, this.getFieldId());
        params.put(ProjectConstants.SESSION_EVENTSRC, this.getPageEventSrc());
        params.put(ProjectConstants.SESSION_EDIT_COLUMN_NAME, this.getEditColumnName());
        params.put(ProjectConstants.SESSION_EDIT_VALUE, this.getFieldValue());
        this.getSession().setAttribute(this.getParametersSessionName(), params);
    }

    public RemarkTextPopup retrieveValues() {
        if (this.getSession().getAttribute(ProjectConstants.SESSION_SELECT_REMARK_TEXT_PARAMETERS) != null) {
            this.setSelectRemarkText(true);
        }
        HashMap params = (HashMap) this.getSession().getAttribute(this.getParametersSessionName());
        this.getSession().removeAttribute(this.getParametersSessionName());
        this.setFieldId((String) params.get(ProjectConstants.SESSION_FIELD));
        this.setPageEventSrc((String) params.get(ProjectConstants.SESSION_EVENTSRC));
        this.setFieldValue((String) params.get(ProjectConstants.SESSION_EDIT_VALUE));
        return this;
    }

}
